package com.sjzs.web.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.sjzs.web.servicIf.RemoteServiceInterface;
import com.sjzs.web.vo.ResultVO;

public class RemoteAccessAction extends ActionSupport {

	private static final long serialVersionUID = 214254753953876438L;
	private static final Logger LOGGER = Logger.getLogger(RemoteAccessAction.class);

	private RemoteServiceInterface serviceHandle;
	private ResultVO result;

	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		StringBuffer sb = new StringBuffer() ; 
		InputStream is = request.getInputStream(); 
		InputStreamReader isr = new InputStreamReader(is,"UTF-8");   
		BufferedReader br = new BufferedReader(isr); 
		String s = "" ; 
		while((s=br.readLine())!=null){ 
		sb.append(s) ; 
		} 
		String str =sb.toString(); 	
		//LOGGER.info("----------------str: " + str); 
		//String cs = request.getParameter("cs");
		//LOGGER.info("----------------cs: " + cs);
		JSONObject jsonObj = JSONObject.fromObject(str);
		String serviceName = jsonObj.getString("serviceName");
		String params = jsonObj.getString("params");
		LOGGER.info("----------------receive serviceName : " + serviceName);
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
		Object o = ctx.getBean(serviceName);
		serviceHandle = (RemoteServiceInterface) o;
		LOGGER.info("-------------------get service " + serviceName + " and run-------------------");
		result = serviceHandle.processResult(params);
		return SUCCESS;
	}

	public ResultVO getResult() {
		return result;
	}

	public void setResult(ResultVO result) {
		this.result = result;
	}

}
